package com.per.note.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.per.note.bean.Count;
import com.per.note.bean.MsgYear;
import com.per.note.bean.User;
import com.per.note.ui.load.MyApplication;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

/**
 * Created by Administrator on 2017/3/16.
 */
public class PieChartDao {
    private Context context;
    List<Count> countList;
    List<MsgYear.ClassMsg> classMsgList;
    SqliteManage sqliteManage = SqliteManage.getInstance(context);

    public PieChartDao(Context context) {
        this.context = context;
    }

    //查询上月支出数据
    public List<MsgYear.ClassMsg> queryLastMonthClass(){
        classMsgList = new ArrayList<>();
        //获得当前年、月信息
        Calendar calendar = Calendar.getInstance(Locale.CHINA);//创建一个日历对象
        int m = calendar.get(Calendar.MONTH) + 1;
        int y = calendar.get(Calendar.YEAR);
        //查询上月支出数据
        SqliteManage.QueryResult result = sqliteManage.query("account", "inout<0 and userid=? and year=? and month=?",
                new String[]{String.valueOf(MyApplication.userid),String.valueOf(y),String.valueOf(m-1)});
        Cursor cursor = result.cursor;
        if(cursor.moveToFirst()){
            do{
                String category = cursor.getString(cursor.getColumnIndex("category"));
                double money = cursor.getDouble(cursor.getColumnIndex("money"));
                MsgYear.ClassMsg classItem = new MsgYear().new ClassMsg();
                classItem.setClassName(category);
                classItem.setMoney(money);
                classMsgList.add(classItem);
            }while (cursor.moveToNext());
        }
        cursor.close();
        return classMsgList;
    }

    //查询上月账户变动数据
    public List<Count> queryLastMonthCount(){
        countList = new ArrayList<>();
        //获得当前年、月信息
        Calendar calendar = Calendar.getInstance(Locale.CHINA);//创建一个日历对象
        int m = calendar.get(Calendar.MONTH) + 1;
        int y = calendar.get(Calendar.YEAR);
        Log.i("----------","当前月："+m+" 年："+y);
        SqliteManage.QueryResult inoutResult = sqliteManage.query("account", "inout<0 and userid=? and year=? and month=?",
                new String[]{String.valueOf(MyApplication.userid),String.valueOf(y),String.valueOf(m)});
        Cursor cursor1 = inoutResult.cursor;
        Log.i("----------","当前cursor1是否为空："+ ((cursor1.moveToFirst())?1:0) );
        if(cursor1.moveToFirst()){
            do{
                String count = cursor1.getString(cursor1.getColumnIndex("count"));
                double money = cursor1.getDouble(cursor1.getColumnIndex("money"));
                int inout = cursor1.getInt(cursor1.getColumnIndex("inout"));
                Count countItem = new Count();
                countItem.setCount(count);
                countItem.setNumber(money);
                countList.add(countItem);
            }while (cursor1.moveToNext());
        }
        Log.i("----------","当前countList长度为："+countList.size());
        cursor1.close();
        return countList;
    }

    //查询全部账户数据
    public List<Count> queryCount(){
        countList = new ArrayList<>();
        SqliteManage.QueryResult result = sqliteManage.query("count", "userid=?", new String[]{String.valueOf(MyApplication.userid)});
        SQLiteDatabase db = result.db;
        Cursor cursor = result.cursor;
        if(cursor.moveToFirst()){
            do{
                String count = cursor.getString(cursor.getColumnIndex("count"));
                double money = cursor.getDouble(cursor.getColumnIndex("money"));
                Count countItem = new Count();
                countItem.setCount(count);
                countItem.setNumber(money);
                Log.i("----","==="+count+"==="+money);
                countList.add(countItem);
            }while (cursor.moveToNext());
        }
        cursor.close();
        return countList;
    }

    //查询全部支出数据
    public List<MsgYear.ClassMsg> queryClass(){
        classMsgList = new ArrayList<>();
        SqliteManage.QueryResult result = sqliteManage.query("account", "inout<0 and userid=?", new String[]{String.valueOf(MyApplication.userid)});
        SQLiteDatabase db = result.db;
        Cursor cursor = result.cursor;
        if(cursor.moveToFirst()){
            do{
                String category = cursor.getString(cursor.getColumnIndex("category"));
                double money = cursor.getDouble(cursor.getColumnIndex("money"));
                MsgYear.ClassMsg classItem = new MsgYear().new ClassMsg();
                classItem.setClassName(category);
                classItem.setMoney(money);
                classMsgList.add(classItem);
            }while (cursor.moveToNext());
        }
        cursor.close();
        return classMsgList;
    }

}
